namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class RoleBLL
    {
        public int Add(Role model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Role(");
            builder.Append("RoleName");
            builder.Append(") values (");
            builder.Append("@RoleName");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleName", model.RoleName) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Role ");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RoleID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string RoleIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Role ");
            builder.Append(" where ID in (" + RoleIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<Role> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM Role ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            Role item = null;
            List<Role> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<Role>();
                foreach (DataRow row in table.Rows)
                {
                    item = new Role();
                    if (row["RoleID"].ToString() != "")
                    {
                        item.RoleID = int.Parse(row["RoleID"].ToString());
                    }
                    item.RoleName = row["RoleName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public Role GetModel(int RoleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RoleID, RoleName  ");
            builder.Append("  from Role ");
            builder.Append(" where RoleID=@RoleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RoleID;
            Role role = new Role();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["RoleID"].ToString() != "")
                {
                    role.RoleID = int.Parse(row["RoleID"].ToString());
                }
                role.RoleName = row["RoleName"].ToString();
                return role;
            }
            return null;
        }

        public bool Update(Role model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Role set ");
            builder.Append(" RoleName = @RoleName  ");
            builder.Append(" where RoleID=@RoleID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RoleID", model.RoleID), new SqlParameter("@RoleName", model.RoleName) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

